{% extends 'layout.html' %}

{% block content %}
    <div class="row">
        <div class="col-md-12">
            <div class="box">
                <div class="box-header with-border">
                    <h3 class="box-title">查看IDC</h3>

                    <div class="box-tools pull-right">
                        <button type="button" class="btn btn-box-tool" data-widget="collapse"><i
                                class="fa fa-chevron-up"></i>
                        </button>
                        <div class="btn-group">
                            <button type="button" class="btn btn-box-tool dropdown-toggle" data-toggle="dropdown">
                                <i class="fa fa-wrench"></i></button>
                            <ul class="dropdown-menu" role="menu">

                            </ul>
                        </div>
                        <button type="button" class="btn btn-box-tool" data-widget="remove"><i class="fa fa-times"></i>
                        </button>
                    </div>
                </div>
                <!-- /.box-header -->
                <div class="box-body">
                    <div class="table-responsive">
                        <div id="toolbar">
                            <a href="{% url 'idc_add' %}" class="btn btn-sm btn-default">
                                <i class="fa fa-plus"></i> 添加IDC
                            </a>
                            <button id="btn_remove" class="btn btn-sm btn-danger">
                                <i class="fa fa-trash-o"></i> 批量删除
                            </button>
                        </div>
                        <table id="table">
                        </table>
                    </div>
                </div>
                <div class="box-footer">
                    <div class="row">
                    </div>
                </div>
            </div>
        </div>
    </div>
{% endblock %}

{% block self_footer_js %}
    <script>
        var $table = $('#table');
        function initTable() {
            $table.bootstrapTable({
                method: 'get',
                url: '{% url 'idc_getlist' %}',
                toolbar: '#toolbar',
                //指定主键列
                idField: 'id',
                sidePagination: "server",
                striped: true,
                search: true,
                showRefresh: true,
                //showColumns: true,
                showExport: true,
                exportDataType: 'basic',
                exportTypes: ['json', 'xml', 'csv', 'txt', 'sql', 'excel'],
                //showPaginationSwitch: true,
                pagination: true,
                pageSize: 10,
                pageList: [10, 25, 50, 100],
                cache: false,
                clickToSelect: true,
                columns: [
                    {
                        field: 'state',
                        checkbox: true,
                        align: 'center'
                    }, {
                        field: 'id',
                        title: 'ID',
                        align: 'center',
                        visible: false
                    }, {
                        field: 'name',
                        title: '机房名',
                        sortable: true,
                        align: 'center'
                    }, {
                        field: 'hostnumber',
                        title: '主机数量',
                        align: 'center'
                    }, {
                        field: 'bandwidth',
                        title: '带宽',
                        align: 'center'
                    }, {
                        field: 'operator',
                        title: '运营商',
                        align: 'center'
                    }, {
                        field: 'contacts',
                        title: '联系人',
                        align: 'center'
                    }
                    , {
                        field: 'phone',
                        title: '电话',
                        align: 'center'
                    }, {
                        field: 'comment',
                        title: '备注',
                        align: 'center'
                    }, {
                        field: 'operate',
                        title: '操作',
                        align: 'center',
                        events: operateEvents,
                        formatter: operateFormatter
                    }

                ]
            })
        }

        //操作列方法
        function operateFormatter(value, row, index) {
            return [
                '<a class="edit btn btn-xs btn-default" href="javascript:void(0)" title="edit">',
                '<i class="fa fa-edit"></i> 编辑',
                '</a>  ',
                '<a class="remove btn btn-xs btn-danger" href="javascript:void(0)" title="Remove">',
                '<i class="fa fa-trash-o"></i> 删除',
                '</a>'
            ].join('');
        }

        window.operateEvents = {
            'click .edit': function (e, value, row, index) {
                //alert('You click like action, row: ' + JSON.stringify(row));
                //Edit(row.id);
                //alert(row.id);
                location.href = '{% url 'idc_edit' %}?id=' + row.id;
            },
            'click .remove': function (e, value, row, index) {
                $table.bootstrapTable('remove', {
                    field: 'id',
                    values: [row.id]
                });
            }
        };

        $(function () {
            initTable();

            //delete button
            $("#btn_remove").click(function () {
                var selected = $table.bootstrapTable('getSelections');
                if ($(selected).length > 0) {
                    BootstrapDialog.confirm({
                        title: "警告",
                        message: "确定删除选中的" + $(selected).length + "个IDC吗？",
                        btnCancelLabel: '放弃',
                        btnCancelClass: 'btn-white',
                        btnOKLabel: '确认',
                        btnOKClass: 'btn-default',
                        callback: function (result) {
                            if (result) {
                                var ids = new Array($(selected).length);
                                $.each(selected, function (index, value) {
                                    ids[index] = value.id;
                                });
                                $.post("{% url 'idc_del' %}", {ids: ids}, function (data) {
                                        if (data.code != 0) {
                                            $table.bootstrapTable("refresh");
                                            dialogItself.close();
                                        }
                                        else BootstrapDialog.alert(data.message);

                                    }, "json"
                                );
                            }
                        }
                    });
                }
                else BootstrapDialog.warning("请选择要删除的行");
            });
        });
    </script>
{% endblock %}